﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using CarLoanSystem.DAL;

namespace CarLoanSystem
{
    public partial class panelCusSaraly : UserControl
    {
        public panelCusSaraly()
        {
            InitializeComponent();
            this.gvLoanInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        public void refreshGridview()
        {
            this.gvLoanInfo.DataSource = LoadLoanAdmin();
        }

        private void genGridview()
        {
            // gen Grideview
            gvLoanInfo.AutoGenerateColumns = false;

            //Set Columns Count
            gvLoanInfo.ColumnCount = 7;

            //Add Columns
            gvLoanInfo.Columns[1].Name = "LOAN_ID";
            gvLoanInfo.Columns[1].HeaderText = "รหัสการขอกู้";
            gvLoanInfo.Columns[1].DataPropertyName = "LOAN_ID";

            gvLoanInfo.Columns[2].Name = "CUSTOMER_NAME";
            gvLoanInfo.Columns[2].HeaderText = "ชื่อ";
            gvLoanInfo.Columns[2].DataPropertyName = "CUSTOMER_NAME";

            gvLoanInfo.Columns[3].Name = "CUSTOMER_LASTNAME";
            gvLoanInfo.Columns[3].HeaderText = "นามสกุล";
            gvLoanInfo.Columns[3].DataPropertyName = "CUSTOMER_LASTNAME";

            gvLoanInfo.Columns[4].Name = "LOAN_DATE";
            gvLoanInfo.Columns[4].HeaderText = "วันที่ขอกู้";
            gvLoanInfo.Columns[4].DataPropertyName = "LOAN_DATE";

            gvLoanInfo.Columns[5].Name = "PRINCIPLE";
            gvLoanInfo.Columns[5].HeaderText = "จำนวนเงินที่กู้";
            gvLoanInfo.Columns[5].DataPropertyName = "PRINCIPLE";

            gvLoanInfo.Columns[6].Name = "DEBT_MONTH_REST";
            gvLoanInfo.Columns[6].HeaderText = "จำนวนเงินที่เหลือ";
            gvLoanInfo.Columns[6].DataPropertyName = "DEBT_MONTH_REST";
            gvLoanInfo.Columns[6].Width = 120;
            // end gen gridview
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                frmCusSaraly1 frmCusSaraly1 = new frmCusSaraly1(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()));
                frmCusSaraly1.ShowDialog();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.gvLoanInfo.DataSource = LoadLoanAdmin();
        }

        private DataTable LoadLoanAdmin()
        {
            DataTable dt = LoanInfoDAL.LoadAllLoanAdmin(this.txtLoanId.Text, this.txtName.Text, this.txtLastName.Text);
            return dt;
        }
    }
}
